package com.example.swimmingpool.controller.goods;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.swimmingpool.entity.goods.Goods;
import com.example.swimmingpool.entity.goods_type.GoodsType;
import com.example.swimmingpool.qo.goods.GoodsQo;
import com.example.swimmingpool.qo.goodsType.GoodsTypeQo;
import com.example.swimmingpool.response.CommonResponse;
import com.example.swimmingpool.service.goods.GoodsService;
import com.example.swimmingpool.service.goods_type.GoodsTypeService;
import com.example.swimmingpool.vo.goods.GoodsGroupByTypeVo;
import com.example.swimmingpool.vo.goods.GoodsVo;
import com.example.swimmingpool.vo.goods_type.GoodsTypeVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @PostMapping("/add")
    public CommonResponse<Boolean> add(@RequestBody Goods goods) {
        var res = goodsService.save(goods);
        return CommonResponse.success(res);
    }

    @PostMapping("/list")
    public CommonResponse<IPage<GoodsVo>> page(@RequestBody GoodsQo qo) {
        var res = goodsService.getPages(qo);
        return CommonResponse.success(res);
    }


    @PostMapping("/update")
    public CommonResponse<Boolean> update(@RequestBody GoodsVo vo) {
        var res = goodsService.update(vo);
        return CommonResponse.success(res != 0);

    }

    @PostMapping("/delete")
    public CommonResponse<Boolean> delete(@RequestBody GoodsVo vo) {
        var res = goodsService.removeById(vo.getId());
        return CommonResponse.success(res);

    }

    @PostMapping("listByType")
    public CommonResponse<List<GoodsGroupByTypeVo>> listByType() {
        return CommonResponse.success(goodsService.getGoodsToUser());
    }

    @PostMapping("/listAll")
    public CommonResponse<List<GoodsVo>> page() {
        var res = goodsService.list();
        return CommonResponse.success(BeanUtil.copyToList(res, GoodsVo.class));
    }
}
